package com.flashcash.solu.broadcast;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.flashcash.solu.harvester.uploadout.UploadUtils;
import com.flashcash.solu.widget.XLeoSp;
import com.google.android.gms.analytics.CampaignTrackingReceiver;

/**
 * 监听并保存安装referrer
 * 测试方法：
 * 1 进到adb shell
 * 2 打开GAv4的log：setprop log.tag.GAv4 VERBOSE
 * 3 发送广播通知：
 *   am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mango.cash/com.filipino.solu.broadcast.InstallReferrerReceiver --es  "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=11&PARTNER_ID=111&PARTNER_CLICK_ID=222"
 */

public class InstallReferrerReceiver extends CampaignTrackingReceiver {

    public static String install_referrer_store_key = "GA_install_referrer_store_key";
    public static String install_referrer_from_ga_sdk_store_key = "install_referrer_from_ga_sdk_store_key";

    public void onReceive(Context context, Intent data) {
        super.onReceive(context, data);

        String referrerValue = getReferrerValue(data.getExtras());

        XLeoSp.getInstance(context).putString(install_referrer_store_key, referrerValue);
    }

    private String getReferrerValue(Bundle bundle){
        String referrerValue = "";

        try{
            if(bundle != null){
                referrerValue = bundle.getString("referrer");
            }
            if(referrerValue == null){
                referrerValue = "";
            }

            if(TextUtils.isEmpty(referrerValue)){
            }else {
            }

        }catch (Exception e){
            UploadUtils.uploadException(e, "InstallReferrerReceiver.getReferrerValue");
        }

        return referrerValue;
    }
}
